package com.googlecode.bushel.obr.xml;

import java.text.ParseException;

import com.googlecode.bushel.core.BundleInfo;
import com.googlecode.bushel.core.BundleRequirement;
import com.googlecode.bushel.obr.filter.AndFilter;
import com.googlecode.bushel.obr.filter.CompareFilter;
import com.googlecode.bushel.obr.filter.NotFilter;
import com.googlecode.bushel.obr.filter.CompareFilter.Operator;
import com.googlecode.bushel.util.Version;
import com.googlecode.bushel.util.VersionRange;

public class RequirementAdapter {

    private Version startVersion = null;

    private boolean startExclusive = false;

    private Version endVersion = null;

    private boolean endExclusive = false;

    private String type = null;

    private String name = null;

    public static void adapt(BundleInfo info, Requirement requirement) throws UnsupportedFilterException,
            ParseException {
        RequirementAdapter adapter = new RequirementAdapter();
        adapter.extractFilter(requirement.getFilter());
        adapter.adapt(info, requirement.isOptional());
    }

    private void extractFilter(RequirementFilter filter) throws UnsupportedFilterException, ParseException {
        if (filter instanceof AndFilter) {
            AndFilter andFilter = (AndFilter) filter;
            for (RequirementFilter subFilter : andFilter.getSubFilters()) {
                extractFilter(subFilter);
            }
        } else if (filter instanceof CompareFilter) {
            CompareFilter compareFilter = ((CompareFilter) filter);
            parseCompareFilter(compareFilter, false);
        } else if (filter instanceof NotFilter) {
            NotFilter notFilter = ((NotFilter) filter);
            if (notFilter.getSubFilter() instanceof CompareFilter) {
                CompareFilter compareFilter = ((CompareFilter) notFilter.getSubFilter());
                parseCompareFilter(compareFilter, true);
            }
        } else {
            throw new UnsupportedFilterException("Unsupported filter: " + filter.getClass().getName());
        }
    }

    private void adapt(BundleInfo info, boolean optional) throws ParseException {
        VersionRange range = getVersionRange();
        String resolution = optional ? "optional" : null;
        if (type == null) {
            throw new ParseException("No requirement actually specified", 0);
        }
        BundleRequirement requirement = new BundleRequirement(type, name, range, resolution);
        info.addRequirement(requirement);
    }

    private VersionRange getVersionRange() {
        VersionRange range = null;
        if (startVersion != null || endVersion != null) {
            range = new VersionRange(startExclusive, startVersion, endExclusive, endVersion);
        }
        return range;
    }

    private void parseCompareFilter(CompareFilter compareFilter, boolean not) throws UnsupportedFilterException {
        String att = compareFilter.getLeftValue();
        if (BundleInfo.PACKAGE_TYPE.equals(att) || BundleInfo.BUNDLE_TYPE.equals(att) || "symbolicname".equals(att)
                || BundleInfo.SERVICE_TYPE.equals(att)) {
            if (not) {
                throw new UnsupportedFilterException("Not filter on requirement comparaison is not supported");
            }
            if (type != null) {
                throw new UnsupportedFilterException("Multiple requirement type are not supported");
            }
            if ("symbolicname".equals(att)) {
                type = BundleInfo.BUNDLE_TYPE;
            } else {
                type = att;
            }
            if (compareFilter.getOperator() != Operator.EQUALS) {
                throw new UnsupportedFilterException("Filtering is only supported with the operator '='");
            }
            name = compareFilter.getRightValue();
        } else if ("version".equals(att)) {
            String v = compareFilter.getRightValue();
            Operator operator = compareFilter.getOperator();
            if (not) {
                switch (operator) {
                case EQUALS:
                    throw new UnsupportedFilterException("Not filter on equals comparaison is not supported");
                case GREATER_OR_EQUAL:
                    operator = Operator.LOWER_THAN;
                    break;
                case GREATER_THAN:
                    operator = Operator.LOWER_OR_EQUAL;
                    break;
                case LOWER_OR_EQUAL:
                    operator = Operator.GREATER_THAN;
                    break;
                case LOWER_THAN:
                    operator = Operator.GREATER_OR_EQUAL;
                    break;
                }
            }
            switch (operator) {
            case EQUALS:
                if (startVersion != null || endVersion != null) {
                    throw new UnsupportedFilterException("Multiple version matching is not supported");
                }
                startVersion = new Version(v);
                startExclusive = false;
                endVersion = new Version(v);
                endExclusive = false;
                break;
            case GREATER_OR_EQUAL:
                if (startVersion != null) {
                    throw new UnsupportedFilterException("Multiple version matching is not supported");
                }
                startVersion = new Version(v);
                startExclusive = false;
                break;
            case GREATER_THAN:
                if (startVersion != null) {
                    throw new UnsupportedFilterException("Multiple version matching is not supported");
                }
                startVersion = new Version(v);
                startExclusive = true;
                break;
            case LOWER_OR_EQUAL:
                if (endVersion != null) {
                    throw new UnsupportedFilterException("Multiple version matching is not supported");
                }
                endVersion = new Version(v);
                endExclusive = false;
                break;
            case LOWER_THAN:
                if (endVersion != null) {
                    throw new UnsupportedFilterException("Multiple version matching is not supported");
                }
                endVersion = new Version(v);
                endExclusive = true;
                break;
            }
        } else {
            throw new UnsupportedFilterException("Unsupported attribute: " + att);
        }

    }
}
